// import React from 'react';
import './App.less';
import { BrowserRouter as Router,Route, Switch, Redirect } from "react-router-dom";
import { lazy, Suspense, useState } from 'react';
import zhCN from 'antd/lib/locale/zh_CN';
import moment from 'moment';
import { render } from 'react-dom';
import { ConfigProvider, DatePicker, message } from 'antd';
import { AuthGuard } from 'spd-components/guards';
import { Auth } from 'spd-request';
import { parse } from 'query-string';
import 'moment/locale/zh-cn';
import 'antd/dist/antd.css';

moment.locale('zh-cn');

const LoginComponent = lazy(() => import('./pages/login/login.component'))
const HomeComponent = lazy(() => import('./pages/home/home'));

function App() {
  return (
    <ConfigProvider locale={zhCN}>
      <Router>
          <Suspense fallback={''}>
            <Switch>
              <Route exact path="/login" render={() => {
                if(Auth.getAccessToken() == null) {
                  return <LoginComponent />;
                } else {
                  let url = '/';
                  let query = parse(window.location.search);
                  if(query && query['to'] && typeof query.to == 'string') {
                    url = atob(query.to);
                  }
                  return <Redirect to={url}/>
                }
              }}/>
              <Route path="/" render={() => {
                if(Auth.getAccessToken() == null) {
                  return <Redirect to={`/login?to=`+ btoa(window.location.pathname + window.location.search)}/>
                } else {
                  return <HomeComponent />
                }
              }} />
            </Switch>
          </Suspense>
      </Router>
    </ConfigProvider>
  );
}

export default App;
